//
//  DiscoverRunloopImageView.m
//  weibo
//
//  Created by qiuming on 15/12/22.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "DiscoverRunloopImageView.h"
#import "DiscoverRunloopCell.h"
static NSString *runloopCellReusedID = @"runloopCellReusedID";

@interface DiscoverRunloopImageView()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView *runloopView;
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (assign, nonatomic) NSUInteger currentIndex;
@property (strong, nonatomic) NSTimer *timer;

@end
@implementation DiscoverRunloopImageView




- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 设置界面
        [self setupUI];
    }
    return self;
}

/// 设置界面
- (void)setupUI {
    
    self.flowLayout.itemSize = self.bounds.size;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.runloopView.showsHorizontalScrollIndicator = NO;
    self.runloopView.showsVerticalScrollIndicator = NO;
    self.runloopView.bounces = NO;
    self.runloopView.pagingEnabled = YES;
    
    [self.runloopView registerClass:[DiscoverRunloopCell class] forCellWithReuseIdentifier:runloopCellReusedID];
    
    self.currentIndex = 0;
    
    [self.runloopView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

// MARK : - UICollectionViewDateSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imagesUrl.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DiscoverRunloopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:runloopCellReusedID forIndexPath:indexPath];
    cell.imageUrl = self.imagesUrl[[self indexWithOffset:indexPath.item] ];
    cell.totolCount = self.imagesUrl.count;
    
    return cell;
}

// MARK: - UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.currentIndex = [self indexWithOffset:offset];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
    [self.runloopView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    [UIView setAnimationsEnabled:NO];
    [self.runloopView reloadItemsAtIndexPaths:@[indexPath]];
    [UIView setAnimationsEnabled:YES];
}

// MARK: - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate:(UIScrollView *)scrollView {
    [self startTimer];
}

- (void)startTimer {
    if (self.timer) {
        return;
    }
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(fire) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)fire {
    NSIndexPath *indexPath = [self.runloopView indexPathsForSelectedItems].lastObject;
    [self.runloopView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(indexPath.item + 1) inSection:0]atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self scrollViewDidEndDecelerating:self.runloopView];
    });
}

- (NSInteger)indexWithOffset:(NSUInteger)offset {
    return (self.currentIndex + offset - 1) % self.imagesUrl.count;
}


- (UICollectionView *)runloopView {
    if (_runloopView == nil) {
        _runloopView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
    }
    return _runloopView;
}

// MARK: - 懒加载
- (UICollectionViewFlowLayout *)flowLayout {
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    }
    return _flowLayout;
}

@end
